package com.card.game;


import java.util.Collections;
import java.util.List;
import java.util.Vector;

import com.google.common.collect.Lists;

/**   
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author siyunlong  
 * @date 2016年8月13日 下午3:15:56 
 * @version V1.0   
 */
public class MMHandUtils {
	public static boolean isHuPai(Vector<Card> cc) {
		List<Integer> sour = transInteger(cc);
		return isHuPai(sour);
	}
	
	public static boolean isHuPai(List<Integer> sour) {
		// System.out.println("MMHandUtils.isHuPai执行");
		Collections.sort(sour);
		return isDanDui(sour) ||fristDuiFind(sour,0) || fristDuiFind2(sour,0) || allIsDui(sour) ;
	}
	
	public static boolean isCanHu(List<Integer> cards,int c) {
		if(cards.contains(c)) {
			return true;
		}
		int index = c%10;
		if(index > 0) {
			if((index >= 2 && cards.contains(c-1))
					|| (index <= 8 &&cards.contains(c+1))
					) {
				return true;
			}
		}
		return false;
	}
	
	public static List<Integer> transInteger(Vector<Card> sour) {
		List<Integer> cards = Lists.newArrayList();
		for (Card c : sour) {
			cards.add(c.getMm());
		}
		return cards;
	}
	
	public static boolean isDanDui(List<Integer> cards) {
		return cards.size() == 2 && cards.get(0).equals(cards.get(1));
	}
	public static boolean fristDuiFind(List<Integer> cards,int minLink) {
		List<Integer> tempSour = Lists.newArrayList(cards);
		List<List<Integer>> same2 = findSameCardBySize(tempSour, 2);
		for (List<Integer> hand : same2) {
			List<Integer> sour2 = Lists.newArrayList(cards);
			for (int i = 0; i < 2; i++) {
				sour2.remove((Integer)hand.get(0));
			}
			findSameCardBySize(sour2, 3);
			int link = findLinkByCards(sour2).size();
			if(link >= minLink && sour2.isEmpty()) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean fristDuiFind2(List<Integer> cards,int minLink) {
		List<Integer> tempSour = Lists.newArrayList(cards);
		List<List<Integer>> same2 = findSameCardBySize(tempSour, 2);
		for (List<Integer> hand : same2) {
			List<Integer> sour2 = Lists.newArrayList(cards);
			for (int i = 0; i < 2; i++) {
				sour2.remove((Integer)hand.get(0));
			}
			int link = findLinkByCards(sour2).size();
			findSameCardBySize(sour2, 3);
			if(link > minLink && sour2.isEmpty()) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean allIsDui(List<Integer> cards) {
		if(cards.size() != 14) {
			return false;
		}
		Collections.sort(cards);
		List<Integer> sour = Lists.newArrayList(cards);
		findSameCardBySize(sour, 4);
		findSameCardBySize(sour, 2);
		return sour.isEmpty();
	}
	public static void removeVector(List<Integer> sour,List<Integer> desc) {
		for (int card : desc) {
			sour.remove((Integer)card);
		}
	}
	public static List<List<Integer>> findLinkByCards(List<Integer> sour) {
		return findLinkByCards(sour,3);
	}
	public static List<List<Integer>> findLinkByCards(List<Integer> sour,int num) {
		List<List<Integer>> returnVector = Lists.newArrayList();
		for (int i = 0; i < sour.size(); i++) {
			if (sour.size() < num || sour.size() - i < num) {
				break;
			}
			int firstCard = sour.get(i);
			List<Integer> linkCardsTemp = Lists.newArrayList();
			linkCardsTemp.add(firstCard);
			for (int j = i + 1; j < sour.size(); j++) {
				if (linkCardsTemp.size() == num) {
					returnVector.add(Lists.newArrayList(linkCardsTemp));
					removeVector(sour, linkCardsTemp);
					linkCardsTemp.clear();
					i = -1;
					break;
				}
				int card = sour.get(j);
				int lastCard = linkCardsTemp.get(linkCardsTemp.size()-1);
				if (lastCard == card - 1 && lastCard/10 == card/10) {
					linkCardsTemp.add(card);
				} else {
					continue;
				}
			}
			if (linkCardsTemp.size() == num) {
				returnVector.add(Lists.newArrayList(linkCardsTemp));
				removeVector(sour, linkCardsTemp);
				linkCardsTemp.clear();
				i = -1;
			}
		}
		return returnVector;
	}
	
	public static List<List<Integer>> findSameCardBySize(List<Integer> sour, int size) {
		List<List<Integer>> desc = Lists.newArrayList();
        int cardsCount = sour.size();
        int currentIndex = cardsCount - 1;
        while (currentIndex > -1) {
            int tempIndex = currentIndex;
            if (tempIndex < (size - 1)) {
                break;
            }
            int[] cards = new int[size];
            for (int i = 0; i < size; i++) {
                cards[i] = sour.get(tempIndex);
                tempIndex--;
            }
            boolean isSame = true;
            for (int i = 0; i < size - 1; i++) {
                if (cards[0] != cards[i + 1]) {
                    isSame = false;
                    break;
                }
            }
            if (isSame) {
                List<Integer> tempVec = Lists.newArrayList();
                for (int i = 0; i < size; i++) {
                    tempVec.add(cards[i]);
                    sour.remove((Integer)cards[i]);
                }
                desc.add(0, Lists.newArrayList(tempVec));
                currentIndex -= size;
            } else {
                currentIndex--;
            }
        }
        return desc;
    }
	
	public static List<List<Integer>> findJianGeByCards(List<Integer> sour) {
		List<List<Integer>> returnVector = Lists.newArrayList();
		for (int i = 0; i < sour.size(); i++) {
			if (sour.size() < 2 || sour.size() - i < 2) {
				break;
			}
			int firstCard = sour.get(i);
			List<Integer> linkCardsTemp = Lists.newArrayList();
			linkCardsTemp.add(firstCard);
			for (int j = i + 1; j < sour.size(); j++) {
				if (linkCardsTemp.size() == 2) {
					returnVector.add(Lists.newArrayList(linkCardsTemp));
					removeVector(sour, linkCardsTemp);
					linkCardsTemp.clear();
					i = -1;
					break;
				}
				int card = sour.get(j);
				int lastCard = linkCardsTemp.get(linkCardsTemp.size()-1);
				if (lastCard == card - 2 && lastCard/10 == card/10) {
					linkCardsTemp.add(card);
				} else {
					continue;
				}
			}
			if (linkCardsTemp.size() == 2) {
				returnVector.add(Lists.newArrayList(linkCardsTemp));
				removeVector(sour, linkCardsTemp);
				linkCardsTemp.clear();
				i = -1;
			}
		}
		return returnVector;
	}
	
	public static void main(String[] args) {
		JinUtils.getAll3Cards(false);
		String source = "1-1;1-1;1-1;8-0;1-9;1-9;8-0;1-9;2-3;8-0;2-4;2-4;2-4;2-4;";
		Vector<Card> cards = CardUtils.createCardsFromStr(source);
		long now = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			System.err.println(QzUtils.isZimo(cards, false));
//			HandUtils.isHuPai(cards);
		}
		System.err.println(System.currentTimeMillis()-now);
	}
}
